package com.example._2025_03_30;

import jakarta.servlet.http.HttpSession;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {
    @RequestMapping("/login")
    public Boolean login(String username, String password, HttpSession session) {
//        进行参数校验
        if (!StringUtils.hasLength(username) || !StringUtils.hasLength(password)) {
            return false;
        }
//        检验账号和密码是否正确
        if ("zhangsan".equals(username) && "123".equals(password)) {
//            存储 session
            session.setAttribute("username", username);
            return true;
        }
        return false;
    }

    @RequestMapping("/getUserInfo")
    public String getUserInfo(HttpSession session) {
        String username = (String) session.getAttribute("username");
        return username == null ? "" : username;
    }
}
